/*
 * Copyright (C) 2004, Velocityme Partnership
 * TaskStateMachineActions.java
 *
 * Created on 09 October 2004, 02:50
 */

package com.velocityme.www.interfaces.nodeactions;

import com.velocityme.interfaces.*;
import com.velocityme.valueobjects.*;
import com.velocityme.www.actionforms.nodeactionforms.TaskStateMachineValueActionForm;
import com.velocityme.www.dto.IntegerString;
import com.velocityme.www.utility.NodeUtil;
import java.util.*;
import javax.servlet.http.*;
import org.apache.struts.action.*;

/**
 *
 * @author  Robert
 */
public class TaskStateMachineActions extends NodeActions {
    
    /** Creates a new instance of TaskStateMachineActions */
    public TaskStateMachineActions() {
    }
    
    public ActionForward show(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskStateMachinePK taskStateMachinePK = new TaskStateMachinePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("taskStateMachineValue", cs.getTaskStateMachineValue(key, taskStateMachinePK));
        return mapping.findForward("ACTION_DISPLAY");
    }
    
    public ActionForward showCreate(ActionMapping mapping, 
                                    ActionForm form,
                                    HttpServletRequest request,
                                    HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskStateMachinePK taskStateMachinePK = new TaskStateMachinePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        session.setAttribute("parentNodePK", nodePK);
        session.setAttribute("unassignedRolePairs", NodeUtil.getAllRolePairs(key, cs));
        session.setAttribute("interestedRolePairs", new ArrayList());
        TaskStateMachineValueActionForm taskStateMachineValueActionForm = new TaskStateMachineValueActionForm();
        session.setAttribute("taskStateMachineActionForm", taskStateMachineValueActionForm);
        TaskStateMachineValue taskStateMachineValue = new TaskStateMachineValue();
        taskStateMachineValue.setNodeValue(new NodeValue());
        session.setAttribute("taskStateMachineValue", taskStateMachineValue);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward showEdit(ActionMapping mapping, 
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) throws Exception {
        NodePK nodePK = (NodePK)request.getAttribute("nodePK");
        TaskStateMachinePK taskStateMachinePK = new TaskStateMachinePK(nodePK.getNodeId());
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        TaskStateMachineValue taskStateMachineValue = cs.getTaskStateMachineValue(key, taskStateMachinePK);
        session.setAttribute("taskStateMachineValue", taskStateMachineValue);
        TaskStateMachineValueActionForm taskStateMachineValueActionForm = new TaskStateMachineValueActionForm();
        taskStateMachineValueActionForm.setName(taskStateMachineValue.getNodeValue().getName());
        taskStateMachineValueActionForm.setDescription(taskStateMachineValue.getNodeValue().getDescription());
        session.setAttribute("taskStateMachineActionForm", taskStateMachineValueActionForm);
        Collection interestedRolePairs = new ArrayList();
        RoleValue[] roleValues = taskStateMachineValue.getInterestedRoleValues();
        for (int i = 0; i < roleValues.length; i++) {
            interestedRolePairs.add(new IntegerString(roleValues[i].getRoleId(), roleValues[i].getNodeValue().getName()));
        }
        Collection allRolePairs = NodeUtil.getAllRolePairs(key, cs);
        allRolePairs.removeAll(interestedRolePairs);
        session.setAttribute("unassignedRolePairs", allRolePairs);
        session.setAttribute("interestedRolePairs", interestedRolePairs);
        return mapping.findForward("ACTION_EDIT");
    }
    
    public ActionForward edit(ActionMapping mapping, 
                              ActionForm form,
                              HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        HttpSession session = request.getSession();
        KeySession key = (KeySession)session.getAttribute("sessionKey");
        RemoteClientSession cs = RemoteClientSessionUtil.getHome().create();
        NodePK nodePK;
        if (request.getParameter("btnSubmit").equals("OK")) {
            TaskStateMachineValue taskStateMachineValue = (TaskStateMachineValue)session.getAttribute("taskStateMachineValue");
            TaskStateMachineValueActionForm taskForm = (TaskStateMachineValueActionForm)form;
            taskStateMachineValue.getNodeValue().setName(taskForm.getName());
            taskStateMachineValue.getNodeValue().setDescription(taskForm.getDescription());
            taskStateMachineValue.setInterestedRoleValues(taskForm.getInterestedRoleValues(cs.getAllRoleValues(key)));
            System.out.println(taskStateMachineValue.getInterestedRoleValues());
            TaskStateMachinePK taskStateMachinePK;
            if (taskStateMachineValue.taskStateMachineIdHasBeenSet()) {
                taskStateMachinePK = taskStateMachineValue.getPrimaryKey();
                cs.taskStateMachineEdit(key, taskStateMachinePK, taskStateMachineValue);
            }
            else {
                NodePK parentNodePK = (NodePK)session.getAttribute("parentNodePK");
                taskStateMachinePK = cs.taskStateMachineCreate(key, parentNodePK, taskStateMachineValue);
            }
            nodePK = new NodePK(taskStateMachinePK.getTaskStateMachineId());
        }
        // Cancel simply displays the original node again
        else {
            nodePK = new NodePK((Integer)session.getAttribute("nodeId"));
        }
        request.setAttribute("nodePK", nodePK);
        return mapping.findForward("ACTION_SUCCESS");
    }
    
}
